/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sporejava;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author tommystaying
 */
public class Environnement {

    private int width = 10;
    private int height = 10;
    private int cycle = 50;
    int herbivore = 0;
    int omnivore = 0;
    int carnivore = 0;
    int vegetaux = 0;
    private ArrayList<EtreVivant> ev = new ArrayList<EtreVivant>();

    public ArrayList<EtreVivant> getEv() {
        return ev;
    }

    public Environnement() {
    }

    public boolean estVide(){
        return ev.size() == 0;
    }
    public Environnement(int width, int height, int cycle) {
        this.width = width;
        this.height = height;
        this.cycle = cycle;
    }
    
    public EtreVivant getProie(EtreVivant e){
        int pos[] = e.getPosition();
        for(EtreVivant e2 : ev){
            int pos2[] = e2.getPosition();
            if(pos[0] == pos2[0] && pos[1] == pos2[1] && !e.getNomEspece().equals(e2.getNomEspece())){
                if(e instanceof Carnivore && e2 instanceof Animal) return e2;
                if(e instanceof Herbivore && e2 instanceof Vegetal) return e2;
                if(e instanceof Omnivore) return e2;
            }
        }
        return null;
    }
    
    public EtreVivant getPartenaire(EtreVivant e){ int pos[] = e.getPosition();
        for(EtreVivant e2 : ev){
            int pos2[] = e2.getPosition();
            if(Math.abs(pos[0] - pos2[0]) < 1 && Math.abs(pos[1] - pos2[1]) < 1 && e.getNomEspece().equals(e2.getNomEspece()) && (!(e instanceof Animal) || !((Animal)e).getSexe().equals(((Animal)e2).getSexe()) ) && e2.getTempsReproduction() == 0){
               return e2;
            }
        }
        return null;
    }

    public void init() {
        for (int i = 0; i < 30; i++) {
            int coord[] = new int[]{(int) (Math.random() * 9), (int) (Math.random() * 9)};
            ev.add(new Vegetal(1, "artichaux", "CCAD", vegetaux++, "CCADBANANE", coord));
        }
        for (int i = 0; i < 20; i++) {
            int coord[] = new int[]{(int) (Math.random() * 9), (int) (Math.random() * 9)};
            ev.add(new Herbivore("POULE", "POULE", "DADA", herbivore++, "CCCCPOULE", coord));
        }
        for (int i = 0; i < 10; i++) {
            int coord[] = new int[]{(int) (Math.random() * 9), (int) (Math.random() * 9)};
            ev.add(new Carnivore("Crocodile", "CROCRODILLLEUUUUU", "XXXX", carnivore++, "XXXXCROOOOCOOOOO", coord));
        }
    }

    public int getHeight() {
        return height;
    }

    public int getWidth() {
        return width;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getCycle() {
        return cycle;
    }

    public void setCycle(int cycle) {
        this.cycle = cycle;
    }
    public void printEnv(){
        for(EtreVivant e : ev){
            if(e.getSante() < 0 ) {System.out.println("Un(e) " + e.getNomEspece() + " est mort.");}
            if(e instanceof Animal && ((Animal)e).getNivFaim() < 0) {System.out.println("Un(e) " + e.getNomEspece() + " est mort(e) de faim.");}
            else{
                if(e instanceof Vegetal){
                    ((Vegetal)e).afficherAnimal();
                }else if(e instanceof Herbivore && e.getSante() > 0){
                    ((Herbivore)e).afficherAnimal();
                }else if(((EtreVivant)e).getSante() > 0){
                    ((Carnivore)e).afficherAnimal();
                }
            }
        }
    }
    public void cycleSuivant(){
       ArrayList<EtreVivant> nvEtre = new ArrayList<EtreVivant>();
       for(EtreVivant e : ev){
           e.setAge(e.getAge() + 1);
           e.setSante(e.getSante() - 5);
           
           if(e.getSante() > 0){
               if(e instanceof Animal){
                   ((Animal)e).setNivFaim(((Animal)e).getNivFaim() + 20);
                   int position[] = e.getPosition();
                   int x = (int) Math.min(10,Math.max(0,Math.round(position[0] + Math.random() * 2 - 1))); 
                   int y = (int) Math.min(10,Math.max(0,Math.round(position[1] + Math.random() * 2 - 1))); 
                   e.setPosition(new int[]{x,y,0});
                   if(((Animal)e).getNivFaim() > 50) {
                       EtreVivant e2 = this.getProie(e);
                       if(e2 != null){
                           System.out.println("Un(e) " + e.getNomEspece() + " mange un(e) " + e2.getNomEspece());
                           ((Animal)e).setNivFaim(0);
                           e2.setSante(0);
                           
                       }
                   }
                   if(e.getTempsReproduction() == 0){
                      EtreVivant e2 = this.getPartenaire(e);
                       if(e2 != null){
                       e.setTempsReproduction(3);
                       System.out.println("2 "+e.getNomEspece() + "s copulent amoureusement.");
                       if(e instanceof Carnivore){
                           nvEtre.add(new Carnivore(e.getNomEspece(), e.getNomEspece(), e.getBaseADN(), e.getIdentifiant() + e2.getIdentifiant(), e.getADN(), e.getPosition()));
                       }
                        if(e instanceof Herbivore){
                           nvEtre.add(new Herbivore(e.getNomEspece(), e.getNomEspece(), e.getBaseADN(), e.getIdentifiant() + e2.getIdentifiant(), e.getADN(), e.getPosition()));
                       }
                            if(e instanceof Vegetal){
                           nvEtre.add(new Vegetal(((Vegetal)e).getZoneReproduction(), e.getNomEspece(), e.getBaseADN(), e.getIdentifiant() + e2.getIdentifiant(), e.getADN(), e.getPosition()));
                       }
                    }
                     
                   }
                   else{
                       e.setTempsReproduction(e.getTempsReproduction() - 1);
                   }
                                     
               }
           }
       }
       for(int i = 0;i < ev.size();i++){
           EtreVivant e = ev.get(i);
           if(e.getSante() < 1 || (e instanceof Animal && ((Animal)e).getNivFaim() > 100)){
               System.out.println("Un(e) " + e.getNomEspece() + " est mort(e)");
               ev.remove(e);
           }
       }
       for(int i = 0;i<nvEtre.size();i++){
           ev.add(nvEtre.get(i));
       }
        try {
            Thread.currentThread().sleep(1000);
        } catch (InterruptedException ex) {
            System.out.println("Erreur sleep");
        }
    }
}
